import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

// 导航页
import Home from '../views/nav/Home.vue'
import Classify from '../views/nav/Classify'
import ShopCart from '../views/nav/ShopCart'
import Myland from '../views/nav/Myland'

// 子页面
import ShopDetail from '../views/pages/ShopDetail'
import LoginWithPwd from '../views/pages/LoginWithPwd'
import pwd from '../components/user/pwd'

import RegistFaster from '../views/pages/RegistFaster'

import AdressEdit from '../views/pages/AddressEdit'
import AddressList from '../views/pages/AddressList'
import CheckOrder from '../views/pages/CheckOrder'
import Searchview from '../views/pages/Searchview'
import OrderList from '../views/pages/OrderList'
import OrderDetail from '../views/pages/OrderDetail'
import Comments from '../views/pages/Comments'
import FeedBack from '../views/pages/FeedBack'
import Setting from '../views/pages/Setting'
import ModifyPassword from '../views/pages/ModifyPassword'
import myComments from '../views/pages/MyComments'
import MessageList from '../views/pages/MessageList'
import FootPrint from '../views/pages/FootPrint'
import Test from '../components/test'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
const routes = [
    {
        path: '/',
        component: Home,
        meta: {
            hasNav: true,
            isKeep: true,
        }
    },
    {
        path: '/search',
        component: Searchview
    },
    //配置的动态路由，可以使用this.$route.params.proid
    {
        path: '/shopDetail/:proid',
        name: 'ShopDetail',
        component: ShopDetail,
        meta: {
            hasNav: false,
            isKeep: false
        }
    },
    {
        path: '/classify',
        name: 'Classify',
        component: Classify,
        meta: {
            hasNav: true,
            isKeep: true
        }
    },
    {
        path: '/cart',
        name: 'ShopCart',
        component: ShopCart,
        meta: {
            hasNav: true,
            isKeep: true
        }
    },
    {
        path: '/my',
        name: 'My',
        component: Myland,
        meta: {
            hasNav: true,
            isKeep: true
        }
    },
    {
        path: '/login',
        name: 'Login',
        component: LoginWithPwd,
        meta: {
            hasNav: false,
            isKeep: false
        }
    },
    {
        path: '/loginpwd',
        name: 'LoginWithPwd',
        component: LoginWithPwd,
        meta: {
            hasNav: false,
            isKeep: false
        }
    },
    {
        path: '/pwd/:tel',
        name: 'PWD',
        component: pwd,
        meta: {
            hasNav: false,
            isKeep: false
        }
    },
    {
        path: '/register',
        name: 'Regist',
        component: RegistFaster,
    }, {
        path: '/address/:action',
        name: 'Adress',
        component: AdressEdit
    }, {
        path: '/addressList',
        name: 'AddressList',
        component: AddressList
    }, {
        path: '/confirm/:mode',
        name: 'Confirm',
        component: CheckOrder
    }, {
        path: '/order',
        component: OrderList
    }, {
        path: '/orderDetail/:orderid',
        component: OrderDetail
    }, {
        path: '/comments/:proid',
        component: Comments
    }, {
        path: '/feedBack',
        component: FeedBack
    }, {
        path: '/setting',
        component: Setting
    }, {
        path: '/modify',
        component: ModifyPassword
    }, {
        path: '/mycomment',
        component: myComments
    }, {
        path: '/message',
        component: MessageList
    }, {
        path: '/footprint',
        component: FootPrint
    }, {
        path: '/test',
        component: Test
    }
]

const router = new VueRouter({
    mode: 'hash',
    routes
})

router.beforeEach((to, from, next) => {
    let index = 0
    switch (to.path) {
        case '/':
            console.log('首页')
            index = 0
            break
        case '/classify':
            console.log('分页')
            index = 1
            break
        case '/cart':
            console.log('购物车')
            index = 2
            break
        case '/my':
            console.log('我的')
            index = 3
            break
        default:
            console.log('普通二级页面')
            next()
            return;
    }
    store.commit('tabs/doActive', index)
    next()
})

//解决路由重复点击报错
VueRouter.prototype.push = function push (location) {
    return originalPush.call(this, location).catch(err => err)
}

export default router
